package com.zrar.ak.gkpt.service.impl.account;

import com.zrar.ak.gkpt.dao.IBusinessAccountBasicInfoDao;
import com.zrar.ak.gkpt.dao.IBusinessAccountExpandInfoDao;
import com.zrar.ak.gkpt.model.bo.BusinessAccountBasicInfoBO;
import com.zrar.ak.gkpt.model.bo.BusinessAccountExpandInfoBO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.account.IBusinessAccountInfoService;
import com.zrar.ak.gkpt.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BusinessAccountInfoService
 * @Description 台账信息Service层
 * @Author liyang
 * @Time 2020/04/02
 */
@Service
public class BusinessAccountInfoService implements IBusinessAccountInfoService {

    @Autowired
    private IBusinessAccountBasicInfoDao businessAccountBasicInfoDao;
    @Autowired
    private IBusinessAccountExpandInfoDao businessAccountExpandInfoDao;


    @Override
    public JsonResult accountBasicInfoSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessAccountBasicInfoBO businessAccountBasicInfoBO = new BusinessAccountBasicInfoBO();
        try {
            businessAccountBasicInfoBO =  CommonTools.jsonStrToObject(formDataJson, BusinessAccountBasicInfoBO.class);
            String unitId = businessAccountBasicInfoBO.getUnitId();
            //判断对应的下级单位是否有已存在台账基本信息，若有则无法执行insert操作，若无可继续执行
            int accountBasicInfoTotal = businessAccountBasicInfoDao.queryExistCountByAccountInfoUnitId(unitId);
            if(accountBasicInfoTotal>0){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_EXISTENT_DATA);
            }else{
                String creatorId = userVO.getUserId();
                Date createTime = new Date();
                String updateUserId = userVO.getUserId();
                Date updateTime = new Date();
                businessAccountBasicInfoBO.setId(CommonTools.getUUID());
                businessAccountBasicInfoBO.setCreatorId(creatorId);
                businessAccountBasicInfoBO.setCreateTime(createTime);
                businessAccountBasicInfoBO.setUpdateUserId(updateUserId);
                businessAccountBasicInfoBO.setUpdateTime(updateTime);
                businessAccountBasicInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int insert = businessAccountBasicInfoDao.insert(businessAccountBasicInfoBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult accountBasicInfoUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessAccountBasicInfoBO businessAccountBasicInfoBO = new BusinessAccountBasicInfoBO();
        String updateUserId = userVO.getUserName();
        Date updateTime = new Date();
        try {
            businessAccountBasicInfoBO = CommonTools.jsonStrToObject(formDataJson, BusinessAccountBasicInfoBO.class);
            BusinessAccountBasicInfoBO accountBasicInfo = businessAccountBasicInfoDao.selectByPrimaryKey(businessAccountBasicInfoBO.getId());

            if(accountBasicInfo==null || BusinessEnum.IS_DELETE_Y.getValue().equals(accountBasicInfo.getIsDelete())){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
            }else if(!accountBasicInfo.getUnitId().equals(businessAccountBasicInfoBO.getUnitId())){
                //如果同一主键的数据库表记录的单位ID和传入数据的单位ID值不一样，则返回业务异常错误
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }else {
                businessAccountBasicInfoBO.setCreatorId(accountBasicInfo.getCreatorId());
                businessAccountBasicInfoBO.setCreateTime(accountBasicInfo.getCreateTime());
                businessAccountBasicInfoBO.setUpdateUserId(updateUserId);
                businessAccountBasicInfoBO.setUpdateTime(updateTime);
                businessAccountBasicInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int i = businessAccountBasicInfoDao.updateByPrimaryKey(businessAccountBasicInfoBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult accountExpandInfoSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessAccountExpandInfoBO businessAccountExpandInfoBO = new BusinessAccountExpandInfoBO();
        try {
            businessAccountExpandInfoBO =  CommonTools.jsonStrToObject(formDataJson, BusinessAccountExpandInfoBO.class);
            String unitId = businessAccountExpandInfoBO.getUnitId();
            //只有当该下级单位存在台账基本信息且不存在台账扩展信息时，才能执行台账扩展信息的新增操作
            int accountBasicInfoTotal = businessAccountBasicInfoDao.queryExistCountByAccountInfoUnitId(unitId);
            int accountExpandInfoTotal = businessAccountExpandInfoDao.queryExistCountByAccountExpandInfoUnitId(unitId);
            if(accountBasicInfoTotal>0 && accountExpandInfoTotal==0){
                String creatorId = userVO.getUserId();
                Date createTime = new Date();
                String updateUserId = userVO.getUserId();
                Date updateTime = new Date();
                businessAccountExpandInfoBO.setId(CommonTools.getUUID());
                businessAccountExpandInfoBO.setCreatorId(creatorId);
                businessAccountExpandInfoBO.setCreateTime(createTime);
                businessAccountExpandInfoBO.setUpdateUserId(updateUserId);
                businessAccountExpandInfoBO.setUpdateTime(updateTime);
                businessAccountExpandInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int insert = businessAccountExpandInfoDao.insert(businessAccountExpandInfoBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }else if(accountExpandInfoTotal>0){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_EXISTENT_DATA);
            }else {
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult accountExpandInfoUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessAccountExpandInfoBO businessAccountExpandInfoBO = new BusinessAccountExpandInfoBO();
        String updateUserId = userVO.getUserName();
        Date updateTime = new Date();
        try {
            businessAccountExpandInfoBO = CommonTools.jsonStrToObject(formDataJson, BusinessAccountExpandInfoBO.class);
            BusinessAccountExpandInfoBO accountExpandInfo = businessAccountExpandInfoDao.selectByPrimaryKey(businessAccountExpandInfoBO.getId());
            if(accountExpandInfo == null || BusinessEnum.IS_DELETE_Y.getValue().equals(accountExpandInfo.getIsDelete())){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
            }else if(!accountExpandInfo.getUnitId().equals(businessAccountExpandInfoBO.getUnitId())){
                //如果同一主键的数据库表记录的单位编码和传入数据的单位编码值不一样，则返回业务异常错误
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }else{
                businessAccountExpandInfoBO.setCreatorId(accountExpandInfo.getCreatorId());
                businessAccountExpandInfoBO.setCreateTime(accountExpandInfo.getCreateTime());
                businessAccountExpandInfoBO.setUpdateUserId(updateUserId);
                businessAccountExpandInfoBO.setUpdateTime(updateTime);
                businessAccountExpandInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int i = businessAccountExpandInfoDao.updateByPrimaryKey(businessAccountExpandInfoBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult accountInfoGetByUnitId(JsonResult jsonResult, String unitId) {
        Map map = new HashMap();
        BusinessAccountBasicInfoBO businessAccountBasicInfoBO = new BusinessAccountBasicInfoBO();
        businessAccountBasicInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        businessAccountBasicInfoBO.setUnitId(unitId);
        BusinessAccountExpandInfoBO businessAccountExpandInfoBO = new BusinessAccountExpandInfoBO();
        businessAccountExpandInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        businessAccountExpandInfoBO.setUnitId(unitId);
        businessAccountBasicInfoBO = businessAccountBasicInfoDao.selectOne(businessAccountBasicInfoBO);
        businessAccountExpandInfoBO = businessAccountExpandInfoDao.selectOne(businessAccountExpandInfoBO);
        map.put("businessAccountBasicInfoBO",businessAccountBasicInfoBO);
        map.put("businessAccountExpandInfoBO",businessAccountExpandInfoBO);
        jsonResult.setData(map);
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    @Override
    public JsonResult deleteAccountInfoByUnitId(JsonResult jsonResult, String unitId, String accountType) {
        /**
         * accountType---台账类型(1:台账基本信息和台账扩展信息执行删除操作 2:台账扩展信息执行删除操作)
         */
        if("1".equals(accountType)){
            businessAccountBasicInfoDao.deleteAccountBasicInfoByUnitId(unitId);
            businessAccountExpandInfoDao.deleteAccountExpandInfoByUnitId(unitId);
        }else if("2".equals(accountType)){
            businessAccountExpandInfoDao.deleteAccountExpandInfoByUnitId(unitId);
        }
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    @Override
    public JsonPageResult getAccountInfoList(JsonPageResult jsonResult, int currentPage, int pageSize, String unitName) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if(StringUtils.isEmpty(unitName)){
            unitName = null;
        }
        Map map = new HashMap();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("unitName",unitName);
        //返回前端的具体的数据
        List list = null;
        //符合展示条件的数据总数
        Integer total = null;
        total = businessAccountBasicInfoDao.queryAccountBasicInfoShowNum(map);
        list = businessAccountBasicInfoDao.pagingQueryAccountBasicInfoList(map);
        //total = accountInformationDao.queryAccountInformationShowNum(map);
        //list = accountInformationDao.pagingQueryAccountInformationList(map);
        if(total==null){
            total=0;
        }
        //计算一共多少页
        Integer pageNum = (total%pageSize==0)?(total/pageSize):(total/pageSize+1);

        jsonResult.setData(list);
        jsonResult.setCurrentPage(currentPage);
        jsonResult.setPageSize(pageSize);
        jsonResult.setTotalCount(total);
        jsonResult.setTotalPage(pageNum);
        if(list == null || list.size()==0){
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
        }else{
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        }
        return jsonResult;
    }

}
